package com.ysm.smfu.fragment;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.activity.My_XiuGai_Activity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.Md5Utils;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.PayPwdEditText;
import com.ysm.smfu.welcome.SharedConfig;

/**
 * Created by Administrator on 2016/9/27.
 */
public class ZhiFuFragment extends MyBaseFragment {
    private PayPwdEditText fEditText;
    //    第一次输入的密码
    private String string;
    //    第二次输入的密码
    private String string2;
    //    警告信息
    private TextView alert_psd;
    private TextView text;
    private String function;
    private String name;
    private String userId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_psd_zhifu);
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sp.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        userId = currShared.getString(SpKey.USERID, "");
//        输入框
        fEditText = (PayPwdEditText) view.findViewById(R.id.fragment_zhifu_psd);
//        文字
        text = (TextView) view.findViewById(R.id.fragment_psd_zhifu_text);
        alert_psd = (TextView) view.findViewById(R.id.alert_psd);
//        第一次输入支付密码
        text.setText("请输入支付密码");
        fEditText.initStyle(R.drawable.edit_num_bg, 6, 0.5f, R.color.color999999, R.color.color999999, 20);

//        输入完成时调用
        fEditText.setOnTextFinishListener(new PayPwdEditText.OnTextFinishListener() {
            @Override
            public void onFinish(String str) {
                if (isFirst) {
                    string = str;
                    isFirst = false;
                    fEditText.clearText();
                    text.setText("请再次输入支付密码");
                } else {
                    string2 = fEditText.getPwdText();
                    if (!TextUtils.equals(string, string2)) {
                        alert_psd.setText("两次密码必须一致");
                        fEditText.clearText();
                        return;
                    }
                    alert_psd.setText("");
                    String md5psd = Md5Utils.encode(string2);
                    upLoadZhiFuPsd(md5psd);
                }
            }
        });
//        按下后退键时的处理
        ((My_XiuGai_Activity) getActivity()).getBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isFirst) {
                    text.setText("请输入支付密码");
                    fEditText.clearText();
                    string = "";
                    string2 = "";
                    isFirst = true;
                } else {
                    ((My_XiuGai_Activity) getContext()).finish();
                }
            }
        });
        return view;
    }

    //是否是第一次输入密码
    boolean isFirst = true;


    /**
     * 上传支付密码
     * 请求地址 : app/user/updatePayPwd
     * 参数 :
     * id: 用户id   sign：签名 payPwd:支付密码
     * nonce：随机数  timestamp：时间戳
     * 签名字段 :
     * Id;nonce;timestamp外加key 秘钥
     */
    private void upLoadZhiFuPsd(String psd) {
        BaseProtocal.builder(true).upLoadZhiFuPsd(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("设置支付密码" ,response);
                String state = JsonUtil.getState(response);
                if (!TextUtils.equals(state, "1")) {
                    ToastUtils.showToast(JsonUtil.getMessage(response));
                    return;
                }
                Bundle bundle = getArguments();
                if (bundle != null) {
                    function = bundle.getString("function");
                }
//                    根据不同选项进入跳转不同页面,从添加银行卡跳转过来
                if (TextUtils.equals(function, "bank"))
                    UIUtils.startActivity(My_XiuGai_Activity.class, "position", 8);
                showDialog();
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        }, psd, userId);
    }

    public void showDialog() {
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setMessage("设置密码成功")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        getActivity().finish();
                    }
                })
                .create();

        dialog.show();
    }
}
